import { createRouter, createWebHistory } from "vue-router";
import routes from './router'

// 2.返回一个 router 实列，为函数，配置 history 模式
const router = createRouter({
  history: createWebHistory(),
  routes,
});

router.beforeEach((to: any, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title;
  }
  let token = localStorage.getItem("token") || "";
  if (token) {
    next();
  } else {
    if (to.path === "/login") {
      next();
    } else {
      next({ path: "/login" });
      console.log(from)
    }
  }
});
// 3.导出路由   去 main.ts 注册 router.ts
export default router